package com.chenfan.common.extension.dict;

import com.chenfan.common.extension.dict.annotation.DictTag;
import lombok.Getter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 字典上下文
 * @author wulg
 * @date 2021-08-21
 **/
public final class DictContext extends ConcurrentHashMap<Object, List<DictContext.Dict>> {

    private List<String> dictTypeList = new ArrayList<>(8);

    private List<String> dictCodeList = new ArrayList<>();

    public void addDict(Object bean, Dict dict) {
        if(!this.containsKey(bean)){
            List<Dict> list = new ArrayList<>(8);
            this.put(bean, list);
        }
        String dictType = dict.getDictType();
        if(!dictTypeList.contains(dictType)) {
            dictTypeList.add(dictType);
        }
        if(ArrayUtils.isNotEmpty(dict.getDictCodes())) {
            for(String dictCode : dict.getDictCodes()) {
                if(dictCodeList.contains(dictCode)) {
                     continue;
                }
                dictCodeList.add(dictCode);
            }
        }
        this.get(bean).add(dict);
    }

    @Override
    public List<Dict> put(Object key, List<Dict> value) {
        throw new UnsupportedOperationException("DictContext Unsupported Operation");
    }

    @Override
    public void putAll(Map<? extends Object, ? extends List<Dict>> map) {
        throw new UnsupportedOperationException("DictContext Unsupported Operation");
    }

    @Override
    public void clear() {
        if(this.size() > 0){
            for(Entry<Object,List<Dict>> entry : this.entrySet()){
                entry.getValue().clear();
            }
        }
        super.clear();
        dictTypeList.clear();
        dictCodeList.clear();
    }

    public List<String> getDictTypeList() {
        return dictTypeList;
    }

    public List<String> getDictCodeList() {
        return dictCodeList;
    }


    /**
     * 字典对象
     */
    @Getter
    public static class Dict {

        private final String separator;

        private Field field;

        private String[] dictCodes;

        private String dictName;

        private String dictValue;

        public Dict(Field field, String name, String value, final String separator) {
            this.field = field;
            this.dictName = StringUtils.defaultIfBlank(getDictTag().dictName(),name + "Name");
            this.dictCodes = StringUtils.split(value, separator);
            this.separator = separator;
            this.dictValue = "";
        }

        public DictTag getDictTag() {
            return field.getDeclaredAnnotation(DictTag.class);
        }

        public String getDictType() {
            return getDictTag().dictType();
        }

        public boolean isReplace() {
            return getDictTag().replace();
        }

        public boolean isChinese() {
            return getDictTag().chinese();
        }

        public void joinDictValue(String dictValue) {
            if(StringUtils.isBlank(dictValue)) {
                return;
            }
            if(StringUtils.isBlank(this.dictValue)) {
                this.dictValue = dictValue;
            } else {
                this.dictValue = this.dictValue + separator + dictValue;
            }
        }
    }
}
